'''
Created on Apr 17, 2010

@author: darkelf
'''
from basic.html_objects import *
from types import FunctionType, MethodType
import compiler
import new

class DBasicObject(object):
    
    def __init__(self):
        self.did = ""
        
    def set_did(self, d_id):
        self.did = d_id
        
    def get_did(self):
        return self.did
    
    def append_function(self, source):
        mod = compiler.parse(source)                                    
        func = mod.node.nodes[0]
        
        d = {}
        exec source.strip() in d
        setattr(self.__class__, func.name, d[func.name])       
        
    def detect_functions(self, obj):
        pass
        
class DFunction(BHObject):
    
    def __init__(self):
        self.func_name = ''
        self.func_body = ''
               
    def set_content(self, content):
        content = content.replace("\r\n","\n")
        result_string = []
        t_count = 0
        wt_count = 0
        def_found = False
        for c in content:
            if def_found:
                if wt_count==t_count:
                    result_string.append(c)
                    if c=='\n':
                        wt_count = 0
                else:
                    wt_count+=1
            else:
                if c=='\n':
                    if len(result_string)>0:
                        def_found = True
                if c=='\t':
                    t_count+=1
                else:
                    result_string.append(c)
        self.func_body = ''.join(result_string)
        
    def detect_functions(self, obj):
        obj.append_function(self.func_body)
        
class DPage(DBasicObject,BasicHtmlPage):
    
    def __init__(self):
        DBasicObject.__init__(self)
        BasicHtmlPage.__init__(self)
        
class DHead(DBasicObject,BasicHtmlHead):
    
    def __init__(self):
        DBasicObject.__init__(self)
        BasicHtmlHead.__init__(self)
        
class DTitle(DBasicObject,BasicHtmlHeadTitle):
    
    def __init__(self):
        DBasicObject.__init__(self)
        BasicHtmlHeadTitle.__init__(self)
        
    def set_content(self, content):
        b_text = BasicHtmlText()
        b_text.set_content(content)
        self.childs.append(b_text)
        
class DBody(DBasicObject,BasicHtmlBody):
    
    def __init__(self):
        DBasicObject.__init__(self)
        BasicHtmlBody.__init__(self)
        
class DJavaScript(DBasicObject,BasicJavaScript):
    
    def __init__(self):
        DBasicObject.__init__(self)
        BasicJavaScript.__init__(self)
        
    def set_content(self, content):
        b_text = BasicHtmlText()
        b_text.set_content(content)
        self.childs.append(b_text)
        
class DImage(DBasicObject,HtmlImage):
    
    def __init__(self):
        DBasicObject.__init__(self)
        HtmlImage.__init__(self)
        
class DFont(DBasicObject,HtmlFont):
    
    def __init__(self):
        DBasicObject.__init__(self)
        HtmlFont.__init__(self)
        
    def set_content(self, content):
        b_text = BasicHtmlText()
        b_text.set_content(content)
        self.childs.append(b_text)
        
class DContainer(DBasicObject,HtmlDiv):
    
    def __init__(self):
        DBasicObject.__init__(self)
        HtmlDiv.__init__(self)
        
    def set_content(self, content):
        b_text = BasicHtmlText()
        b_text.set_content(content)
        self.childs.append(b_text)
        
class DTable(DBasicObject,HtmlTable):
    
    def __init__(self):
        DBasicObject.__init__(self)
        HtmlTable.__init__(self)
        
class DTRow(DBasicObject,HtmlTableRow):
    
    def __init__(self):
        DBasicObject.__init__(self)
        HtmlTableRow.__init__(self)
        
class DTData(DBasicObject,HtmlTableData):
    
    def __init__(self):
        DBasicObject.__init__(self)
        HtmlTableData.__init__(self)
        
    def set_content(self, content):
        b_text = BasicHtmlText()
        b_text.set_content(content)
        self.childs.append(b_text)